#!/bin/bash

set -ex
set -o pipefail

#PACKAGES=${WORK_DIR}/$(date "+%Y_%m%d_llvm15_release")

if [ -z "$1" ]
then
		BRANCH='master'
else
		BRANCH=$1
fi

WORK_DIR=$(cd "$(dirname $0)";pwd)
PACKAGES=${WORK_DIR}/${BRANCH}
LLVM_DIR=/srv/workspace/llvm-release

docker exec -it  llvm-build /bin/bash -c "cd ${LLVM_DIR} && bash -x build.sh ${BRANCH}"
rm -rf ${PACKAGES}
mkdir -p ${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/${BRANCH}/packages ${PACKAGES}
docker cp llvm-build:${LLVM_DIR}/repo.log /${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/env_prepare.log /${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/linux_aarch64_build.log /${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/llvm_build.log /${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/ohos_build.log /${PACKAGES}/log
docker cp llvm-build:${LLVM_DIR}/${BRANCH}/toolchain/llvm-project/llvm-build/platform_package.sh ${PACKAGES}/packages

sed -i '
/^commit_id=""/ {
    N
    /^commit_id=""\ndate=""/ {
        c\
if [ -z "$1" ]\
then\
        BRANCH='\''master'\''\
else\
        BRANCH=$1\
fi\
commit_id=`docker exec -it  llvm-build /bin/bash -c "cd '"${LLVM_DIR}"'/${BRANCH}/toolchain/llvm-project && git rev-parse --short HEAD"`\
commit_id=$(echo $commit_id | sed '\''s/\r//g'\'')\
commit_id=$(echo ${commit_id:0:6})\
date=""
    }
}' ${PACKAGES}/packages/platform_package.sh

echo "${PACKAGES}"
